package com.shaoshaohuo.app.ui.ec;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.EcCategoryThird;
import com.shaoshaohuo.app.entity.Specification;
import com.shaoshaohuo.app.entity.SpecificationEntity;
import com.shaoshaohuo.app.net.HttpRequest;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.utils.SpecificationDataUtil;
import com.shaoshaohuo.app.view.SelectSpecificationsView;
import com.shaoshaohuo.app.view.TopbarView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 *
 * 规格 选择页面
 * Created by cao on 2016/2/22.
 */
public class SelectSpecificationsActivity extends BaseActivity {

    public static final int SELECT_OK = 100;

    private LinearLayout contentView;
    private Map<Specification, List<Specification>> data;
    private List<SelectSpecificationsView> selectSpecificationsViews = new ArrayList<>();

    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_specifications);
        initView();
        initData();
    }

    private void initView() {
        contentView = (LinearLayout) findViewById(R.id.ll_content);
        TopbarView topbar = (TopbarView) findViewById(R.id.topbar);
        topbar.setCenterText("选择产品规格");
        topbar.setLeftView(true, true);
        topbar.setRightText("确定");
        topbar.setRightClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Map<Specification, List<Specification>> result = new HashMap<Specification, List<Specification>>();
                for (SelectSpecificationsView view : selectSpecificationsViews) {
                    Map<Specification , List<Specification>> map = view.getSelectValues();
                    if (!map.isEmpty()) {
                        result.putAll(map);
                    }
                }
                Intent intent = new Intent();
                intent.putExtra(ExtraName.isForResult, (Serializable) result);
                setResult(SELECT_OK, intent);
                finish();
            }
        });
    }

    private void initData() {
        Intent intent = getIntent();
        EcCategoryThird ecCategoryThird = (EcCategoryThird) intent.getSerializableExtra(ExtraName.ecCategoryThird);
        getData(ecCategoryThird.getBigpid(), ecCategoryThird.getPid(), ecCategoryThird.getCatid());
    }

    public void getData(String bigpid, String pid, String catid) {
        startLoadingDialog();
        RequestService.getInstance().getSpecifications(this, bigpid, pid, catid, SpecificationEntity.class, new HttpRequest.RequestListener() {
            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if (resultData.isOk()) {
                    SpecificationEntity entity = (SpecificationEntity) resultData;
                    if (entity.data == null) {
                        showToast("此产品暂时还没有规格供选择");
                        finish();
                        return;
                    }
                    updataUi(entity.data.list);
                } else {
                    showToast(resultData.getMsg());
                }
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(R.string.please_check_network);
            }
        });
    }

    public void updataUi(List<Specification> list) {
        data  = SpecificationDataUtil.list2map(list);

        for (Map.Entry<Specification, List<Specification>> entry : data.entrySet()) {
            SelectSpecificationsView selectSpecificationsView = new SelectSpecificationsView(this);
            selectSpecificationsView.setData(entry.getKey(), entry.getValue(), "2".equals(entry.getKey().is_checkbox));
            contentView.addView(selectSpecificationsView);
            selectSpecificationsViews.add(selectSpecificationsView);
        }
    }

    
}
